/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.controller;

import com.cxhd.pacs.DataConfig;
import com.cxhd.pacs.StaticData;
import com.cxhd.pacs.external.IntefaceOther;
import com.cxhd.pacs.model.Asklist;
import com.cxhd.pacs.tools.HttpClient;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

/**
 *
 * @author 52461
 */
public class AddAskCtrl extends Controller {

    private Asklist asklist = new Asklist();
    private Map<String, String> config = DataConfig.getInstance().getConfig();

    public void index() {

        render("/view/addasklist.jsp");

    }

    public void getDoctor() {
       
        renderJson(IntefaceOther.getDoctorByGroup(StaticData.DOCTOR_GROUP_NAME));
    
        //renderJson(IntefaceOther.getCurrentDayDoctor(getPara("date")));
    }

    public void getList() {
        String sql = "select b.*,max(event_time),c.name as hos_name ";
        String serarchContent = getPara("searchContent", null);
        String param = "from  host_detail a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code group by a.host_id  order by a.event_time desc";
        if (serarchContent != null) {
            param = "from host_detail a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code where a.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or a.info_id like '%" + serarchContent + "%' group by a.host_id order by a.event_time desc";

        }
        System.out.println(sql + param);
        Page<Record> userPage = Db.paginate(getParaToInt("currentPage"), getParaToInt("pageSize", 10), sql, param);

        renderJson(userPage);
    }

    public void saveAsk() {
        try {
           String pk = asklist.saveAsk(this);

            //是否发送短信通知
            if (config.get("smsurl") != null && config.get("smsflag") != null && config.get("smsflag").equalsIgnoreCase("true")) {

                String content = " 病人名字：" + getPara("patient_name") + "，性别：" + getPara("sex") + "，年龄：" + getPara("age") + "，时间：" + getPara("appointment_time") + " ";
                content += " weixin.yxrmyy.cn/wlmz/p?h=" + getPara("patient_id")+"&s="+pk;
                String to = getPara("recvicer_phone");
                if (to != null) {
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("phone",to));
                    params.add(new BasicNameValuePair("content", content));
                   // System.out.println("phone:"+to+"_______content:"+content);
                    HttpClient.httpPost("http://"+config.get("smsurl")+"/SmsServer/smsForwangluomenzhen.jsp", params);
                }

            }

            renderText("true");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("false");
        }
    }

    public void getAskList() {
        Map<String, String> info = (Map<String, String>) getSession().getAttribute("CX_JSESSION_USER");
        String sql = "select * ";

        String param = "from  asklist where sender_id='" + info.get("uuid") + "' or sender_id='" + info.get("username") + "' order by createtime desc";
//        if (serarchContent != null) {
//            param = "from host_detail a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code where a.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or a.info_id like '%" + serarchContent + "%' group by a.host_id order by a.event_time desc";
//
//        }
        System.out.println(sql + param);
        Page<Record> userPage = Db.paginate(getParaToInt("currentPage"), getParaToInt("pageSize", 10), sql, param);

        renderJson(userPage);
    }

    public void deleteItem() {
        if (Db.findFirst("select * from asklist where pk='" + getPara("pk") + "' and status=0") != null) {
            Db.deleteById("asklist", "pk", getPara("pk"));
            renderText("true");
        } else {
            renderText("false");
        }
    }
    public void viewApply(){
        redirect("/main/view/ask/viewApply", true);
    }
    
}
